# Build recipe for lvm2.
#
# Copyright (c) 2018-2019, 2022 Matias Fonzo, <selk@dragora.org>.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Exit immediately on any error
set -e

program=LVM2
version=2.02.168
release=3

# Define a category for the output of the package name
pkgcategory=tools

pkgname=lvm2

tarname=${program}.${version}.tgz

# Remote source(s)
fetch=https://sourceware.org/ftp/lvm2/releases/$tarname

description="
Logical Volume Management tools.

LVM2 refers to the userspace toolset that provide
logical volume management facilities on GNU/linux.
"

homepage=https://www.sourceware.org/lvm2
license="GPLv2+, LGPL2.1"

# Source documentation
docs="COPYING* README VERSION VERSION_DM WHATS_NEW WHATS_NEW_DM"
docsdir="${docdir}/${program}-${version}"

# The package has a custom source directory
srcdir=${program}.${version}

build()
{
    unpack "${tardir}/$tarname"

    cd "$srcdir"

    # Set sane permissions
    chmod -R u+w,go-w,a+rX-s .

    # Apply patches from Alpine Linux in order to build against Musl
    patch -p1 < "${worktree}/patches/lvm2/fix-stdio-usage.patch"
    patch -p1 < "${worktree}/patches/lvm2/mallinfo.patch"
    patch -p1 < "${worktree}/patches/lvm2/library_dir-default-config.patch"
    patch -p1 < "${worktree}/patches/lvm2/mlockall-default-config.patch"

    # --enable-static_link does not work in this version
    ./configure CPPFLAGS="$QICPPFLAGS" CFLAGS="$QICFLAGS" \
     $configure_args \
     --libdir=/usr/lib${libSuffix} \
     --with-usrlibdir=/usr/lib${libSuffix} \
     --with-udev-prefix="" \
     --mandir=$mandir \
     --docdir=$docsdir \
     --enable-pkgconfig \
     --enable-applib \
     --enable-cmdlib \
     --enable-dmeventd \
     --enable-udev_rules \
     --enable-udev_sync \
     --disable-readline \
     --with-lvm1=internal \
     --with-thin=internal \
     --with-device-uid=0 \
     --with-device-gid=11 \
     --with-device-mode=0660 \
     --build="$(gcc -dumpmachine)"

    make -j${jobs} DESTDIR="$destdir" install

    # Strip remaining binaries and libraries
    find "$destdir" -type f | xargs file | \
     awk '/ELF/ && /executable/ || /shared object/' | \
      cut -f 1 -d : | xargs strip --strip-unneeded 2> /dev/null || true

    # Manage .new (config) files via graft(8)
    touch "${destdir}/etc/lvm/.graft-config"

    # Compress and link man pages (if needed)
    if test -d "${destdir}/$mandir"
    then
        (
            cd "${destdir}/$mandir"
            find . -type f -exec lzip -9 {} +
            find . -type l | while read -r file
            do
                ln -sf "$(readlink -- "$file").lz" "${file}.lz"
                rm -- "$file"
            done
        )
    fi

    # Copy documentation
    mkdir -p "${destdir}/$docsdir"
    cp -p $docs "${destdir}/$docsdir"
}

